<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	function Login(){
		parent::__construct();
		
		session_start();
		
		$this->load->model('cms_model');
	}
	
	public function index()
	{
		$data['tip'] = '';
		$this->load->view('login_view',$data);
	}
	
	public function drawVC()
	{		
		$this->load->library('captchaimage');
		 
        ob_start();
        
        //write session
        $vc = $this->captchaimage->getString();        
        $_SESSION['vcode'] =  $vc;
        
        $this->captchaimage->setFont('./fonts/1.ttf',20);     
        $this->captchaimage->draw();  
              
        ob_flush();
	}
	
	public function dologin()
	{
		if(!isset($_POST['txtVC'])|| !isset($_POST['userid']) || !isset($_POST['pwd']))
			return ;
			
		if (!isset($_SESSION['vcode']))
			return;
			
		if ($_SESSION['vcode'] === strtoupper($_POST['txtVC'])){
            $r = $this->cms_model->Login($_POST['userid'],$_POST['pwd']);
            if($r){
            	$_SESSION['qf_userid'] = strtolower($_POST['userid']);
            	redirect('/cms');
            }
            else{
            	$data['tip'] = '用户名密码不匹配!';
            	$this->load->view('login_view',$data);
            }           
        } else {
            $data['tip'] = '验证码不正确!';
            $this->load->view('login_view',$data);
        }
	}
}